﻿/*-------------------------------------------------------------------------
* 命名空间: XrayDirection.DAL.LocalDataAccess/EapParamsAccess 
* 类   名: EapParamsAccess
* 功   能: N/A
* 时   间: 2024-08-27 9:20:35
* 版   本: V1.0
* 作   者: Fioman
* 格   言: Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using XrayDirection.Common;
using XrayDirection.DAL.DataEntity;
using XrayDirection.Extensions;

namespace XrayDirection.DAL.LocalDataAccess
{
    public class EapParamsAccess
    {
        /// <summary>
        /// 获取EapParams表中所有的数据,虽然只有一条数据,但是还是要用复数,主要是为了防止程序中要在DataGrid中使用
        /// </summary>
        /// <returns></returns>
        public static DataResult<List<EapParamsEntity>> GetEapParams()
        {
            DataResult<List<EapParamsEntity>> result = new DataResult<List<EapParamsEntity>>();
            try
            {
                using (var context = new DataContext())
                {
                    result.Data = context.EapParams.OrderBy(data => data.Id).ToList();
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }


        /// <summary>
        /// 添加默认数据
        /// </summary>
        /// <param name="eapParam"></param>
        /// <returns></returns>
        public static DataResult AddEapParams(EapParamsEntity eapParams)
        {
            DataResult result = new DataResult();
            try
            {
                using (var context = new DataContext())
                {
                    context.Add(eapParams);
                    int affectedRows = context.SaveChanges();
                    if (affectedRows > 0)
                    {
                        result.State = true;
                    }
                    else
                    {
                        result.Message = ConstantManager.DatabaseAddError;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }


        public static DataResult UpdateEapParams(EapParamsEntity eapParams)
        {
            DataResult result = new DataResult();
            try
            {
                using (var context = new DataContext())
                {
                    // 查询要更新的数据是否存在
                    var existingData = context.EapParams.FirstOrDefault(data => data.Id == eapParams.Id);
                    if (existingData != null)
                    {
                        // 更新实体属性
                        context.Entry(existingData).CurrentValues.SetValues(eapParams);
                        int affectedRows = context.SaveChanges();
                        if (affectedRows > 0)
                        {
                            result.State = true;
                        }
                        else
                        {
                            result.State = true;
                        }
                    }
                    else
                    {
                        result.Message = $"更新失败,要更新的数据不存在,更新的数据Id = {eapParams.Id}";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }
    }
}
